package com.mzj;

import java.lang.reflect.Field;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.elasticsearch.client.RestHighLevelClient;

import com.mzj.mqtt.ScaleMachineVo;
import com.mzj.solr.MSolrClient;
import com.mzj.solr.SolrCursorHbMove;
import com.mzj.solr.SubscribeLogMove;

public class MoveMain {
	
	private static final Logger LOG = Logger.getLogger(MoveMain.class);  
	 
	 public static void main(String[] args) {
		 
		 Field[] fields = ScaleMachineVo.class.getDeclaredFields();  
			for (Field tempField : fields) {
				String name = tempField.getName();
				System.out.println(name);
			}
		 
			if(args==null || args.length==0) {
				LOG.error("Parameter is null");
				return;
			}
			String type = args[0];
			LOG.info("--------------type:"+type);
			if("delete".equals(type)) {
			}else if("delId".equals(type)){
			}else if("move".equals(type)){
				String indexName = args[1];
				Integer size = Integer.valueOf(args[2]);
				RestHighLevelClient esClient = MSolrClient.getEsClient();
				SolrClient solrClient = MSolrClient.getClient(indexName);
				int start = 0;
				Long total = SolrCursorHbMove.getTotal(solrClient,indexName);
				try {
					do {
						LOG.info("--------------total:"+total);
						LOG.info("Start move data : " + start);
						SolrCursorHbMove.move(solrClient, esClient, indexName, size, indexName, start);
						start = start+size;
						LOG.info("End move data : " + start);
						LOG.info("----------------total : " + total+",start"+start);
					}while(start<=total);
				} catch (Exception e) {
					LOG.error(e.getMessage(),e);
				}finally {
					try {
					if(esClient!=null) esClient.close();
					if(solrClient!=null) solrClient.close();
					}catch(Exception e) {
						LOG.error(e.getMessage(),e);
					}
				}
			}else if("other".equals(type)){
				String indexName = args[1];
				Integer size = Integer.valueOf(args[2]);
				RestHighLevelClient esClient = MSolrClient.getEsClient();
				SolrClient solrClient = MSolrClient.getClient(indexName);
				int start = 0;
				Long total = SubscribeLogMove.getTotal(solrClient,indexName);
				try {
					do {
						LOG.info("--------------total:"+total);
						LOG.info("Start move data : " + start);
						SubscribeLogMove.move(solrClient, esClient, indexName, size, indexName, start);
						start = start+size;
						LOG.info("End move data : " + start);
						LOG.info("----------------total : " + total+",start"+start);
					}while(start<=total);
				} catch (Exception e) {
					LOG.error(e.getMessage(),e);
				}finally {
					try {
					if(esClient!=null) esClient.close();
					if(solrClient!=null) solrClient.close();
					}catch(Exception e) {
						LOG.error(e.getMessage(),e);
					}
				}
			}else if("hbmove".equals(type)){
			}else {
				LOG.error("The operation type does not exist");
				return;
			}
		}
	 
	 

	/*public static void main(String[] args) {
		if(args==null || args.length==0) {
			LOG.error("Parameter is null");
			return;
		}
		String type = args[0];
		if("delete".equals(type)) {
			String index = args[1];
			try {
				if(StringUtils.isEmpty(index)) {
					LOG.error("Index is null");
					return;
				}
				SolrDelete.delete(index);
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}
		}else if("delId".equals(type)){
			String index = args[1];
			if(StringUtils.isEmpty(index)) {
				LOG.error("Index is null");
				return;
			}
			String id = args[2];
			if(StringUtils.isEmpty(id)) {
				LOG.error("Id is null");
				return;
			}
			SolrClient client = MSolrClient.getClient(index);
			try {
				client.deleteById(id);
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}finally {
				try {
					if(client!=null) client.close();
				} catch (IOException e) {
					LOG.error(e.getMessage(),e);
				}
			}
		}else if("move".equals(type)){
			try {
				String index = args[1];
				if(StringUtils.isEmpty(index)) {
					LOG.error("Index is null");
					return;
				}
				
				int[] keyNums  = {0,1,2,3,4,5,6,7,8,9}; 
				SolrClient client = MSolrClient.getClient(index);
				Long total = SolrMove.getTotal(client,index);
				client.close();
			
				for(int keyNum : keyNums) {
					new Thread(new SolrMoveThread(index,keyNum,total)).start();
				}
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}
		}else if("curmove".equals(type)){
			SolrClient client = null;
			SolrClient cloudClient = null;
			try {
				String indexName = args[1];
				if(StringUtils.isEmpty(indexName)) {
					LOG.error("Index is null");
					return;
				}
				client = MSolrClient.getClient(indexName);
				cloudClient = MSolrClient.getCloudClient();
				SolrCursorQuery.cursorQuery(client, cloudClient, indexName);
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}finally {
				try {
					if(client!=null) client.close();
					if(client!=null) cloudClient.close();
				} catch (IOException e) {
					LOG.error(e.getMessage(),e);
				}
			}
		}else if("hbmove".equals(type)){
			SolrClient client = null;
			SolrClient cloudClient = null;
			try {
				String indexName = args[1];
				if(StringUtils.isEmpty(indexName)) {
					LOG.error("Index is null");
					return;
				}
				client = MSolrClient.getClient(indexName);
				cloudClient = MSolrClient.getCloudClient();
				SolrCursorHbMove.cursorQuery(client, cloudClient, indexName);
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}finally {
				try {
					if(client!=null) client.close();
					if(client!=null) cloudClient.close();
				} catch (IOException e) {
					LOG.error(e.getMessage(),e);
				}
			}
		}else {
			LOG.error("The operation type does not exist");
			return;
		}
	}*/

}
